/*
 * Copyright (c) 2017,重庆阿莫比科技有限公司,All Rights Reserved.
 */
package com.wowocai.btchina.resolver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.wowocai.btchina.entity.ResponseResultEntity;
import com.wowocai.btchina.enums.ApiResultEnums;

/**
 * <br/>
 * Date: 2017年2月28日 上午11:26:00 <br/>
 * 
 * @Author 刘建成
 * @Version
 */
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(RestExceptionHandler.class);

    /**
     * 处理@RequestParam错误, 即参数不足
     * 
     * @return
     */
    @Override
    protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        return new ResponseEntity<Object>(new ResponseResultEntity(ApiResultEnums.PARA_ERRORS), status);
    }

    /**
     * 404
     * TODO The function of this method is described here - Optional.
     * 
     * @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,
     * org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus,
     * org.springframework.web.context.request.WebRequest)
     */
    @Override
    protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
        ResponseResultEntity result = new ResponseResultEntity(ApiResultEnums.PAGE_NOT_FOUND);
        return handleExceptionInternal(ex, result, headers, status, request);
    }

    /**
     * 处理500错误
     * 
     * @return
     */
    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
        // logger.error("服务器内部错误 : ", ex);
        ResponseResultEntity result = null;
        // 请求方式不支持
        if (ex instanceof HttpRequestMethodNotSupportedException) {
            result = new ResponseResultEntity(ApiResultEnums.REQUEST_METHOD_UNSUPPORTED);
            return new ResponseEntity<Object>(result, status);
        }
        if (body == null) {
            result = new ResponseResultEntity(ApiResultEnums.SYS_ERRORS);
            return new ResponseEntity<Object>(result, status);
        } else {
            return new ResponseEntity<Object>(body, status);
        }
    }

    /**
     * 处理参数类型转换失败
     * 
     * @param request
     * @return
     */
    @Override
    protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
        logger.error("参数类型不匹配");
        return new ResponseEntity<Object>(new ResponseResultEntity(ApiResultEnums.PARA_ERRORS), status);
    }

}
